<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *空调管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'edit','title'=>'新建空调分类'),	
);
$onepluginname='jy_kongtiaoshop';
//空调分类
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'mid'=>0);
	$params['parentid']=intval($_RQ['id']);
	$return['params']['parentid'] = $params['parentid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['id'])){
		$return['params']['parentid']=intval($_RQ['id']);
	}
	if(!empty($_RQ['parentid'])){
		$params['parentid']=intval($_RQ['parentid']);
	}
	$tip='[温馨提示]：一级是品牌分类比如：海尔，美的，格力
	</br><red>二级，可以设置类型，比如：内机，外机，线控器
	';
	$params['onepluginname']=$onepluginname;
	$parenttitle=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?$parenttitle.'子':'';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','parentid','onepluginname','pluginname','uniacid','title','isshow','isrecommand','image','paixu');
	$list=pdo_getslice('jy_weishop_goods_group',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	if($list){
		foreach($list as &$item){
			$item['onepluginnametitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'公共分类';
			$item['issetonepluginname']=$pluginttitle?1:0;
			if($item['parentid']){
				$item['duanurl']='jy_kongtiaoshop/goods.index//cid='.$item['id'];
				$item['url']=custom_url('jy_kongtiaoshop/goods.index//cid='.$item['id'],true);
				$item['duanurl2']='jy_kongtiaoshop/shop.groups//pid='.$item['parentid'].'&cid='.$item['id'];
			}else{
				$item['duanurl']='jy_kongtiaoshop/goods.index//pid='.$item['id'];
				$item['url']=custom_url('jy_kongtiaoshop/goods.index//pid='.$item['id'],true);
				$item['duanurl2']='jy_kongtiaoshop/shop.groups//pid='.$item['id'];
			}
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'品牌分类列表'),
			//array('op'=>'otherlist','title'=>'其他场景分类'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'tip'=>$tip,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'image','title'=>'品牌分类图片','type'=>'image'),
			array('name'=>'title','title'=>'品牌分类名称','type'=>'string'),
			array('name'=>'url','title'=>'品牌分类链接','type'=>'copy'),
			array('name'=>'duanurl','title'=>'品牌分类短链接','type'=>'copy'),
			array('name'=>'duanurl2','title'=>'品牌分类短链接2','type'=>'copy'),
			array('name'=>'isshow','title'=>'是否显示','type'=>'switch','set'=>'set'),
			array('name'=>'isrecommand','title'=>'是否推荐(首页)','type'=>'switch','set'=>'set'),
		),'action'=>array(
			array('type'=>'href','op'=>'list2','title'=>'类型管理','show'=>array('parentid'=>0)),
		)),
		'search_one'=>array('name'=>'title','title'=>'空调品牌分类名称','add'=>false),
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}

//空调分类
if($_RQ['op']=='list2'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0);
	$params['parentid']=intval($_RQ['id'])?:intval($_RQ['parentid']);
	$return['params']['parentid'] = $params['parentid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$parenttitle=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','parentid','onepluginname','pluginname','uniacid','title','isshow','isrecommand','image','paixu');
	$list=pdo_getslice('jy_weishop_goods_group',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	if($list){
		foreach($list as &$item){
			$item['onepluginnametitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'公共分类';
			$item['issetonepluginname']=$pluginttitle?1:0;
			if($item['parentid']){
				$item['duanurl']='jy_weishop/goods.index//cid='.$item['id'];
				$item['url']=custom_url('jy_weishop/goods.index//cid='.$item['id'],true);
				$item['duanurl2']='jy_weishop/shop.groups//pid='.$item['parentid'].'&cid='.$item['id'];
			}else{
				$item['duanurl']='jy_weishop/goods.index//pid='.$item['id'];
				$item['url']=custom_url('jy_weishop/goods.index//pid='.$item['id'],true);
				$item['duanurl2']='jy_weishop/shop.groups//pid='.$item['id'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'品牌分类'),
			array('op'=>'list2','title'=>$parenttitle.'分类列表'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'image','title'=>'分类图片','type'=>'image'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
			array('name'=>'url','title'=>'分类链接','type'=>'copy'),
			array('name'=>'duanurl','title'=>'分类短链接','type'=>'copy'),
			array('name'=>'duanurl2','title'=>'分类短链接2','type'=>'copy'),
			array('name'=>'isshow','title'=>'是否显示','type'=>'switch','set'=>'set'),
			array('name'=>'isrecommand','title'=>'是否推荐(首页)','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'title','title'=>'空调分类名称','add'=>false),
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}
if($_RQ['op']=='list3'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0);
	$params['parentid']=intval($_RQ['id'])?:intval($_RQ['parentid']);
	$return['params']['parentid'] = $params['parentid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$parenttitle=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'title');
	$parenttitle=$parenttitle?'【'.$parenttitle.'】子':'';
	$parentid2=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$params['parentid']),'parentid');
	$parenttitle2=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$parentid2),'title');
	$parenttitle2=$parenttitle2?'【'.$parenttitle2.'】子':'';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','parentid','onepluginname','pluginname','uniacid','title','isshow','isrecommand','image','paixu');
	$list=pdo_getslice('jy_weishop_goods_group',$params,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	if($list){
		foreach($list as &$item){
			$item['onepluginnametitle']=$item['onepluginname']?pdo_getcolumn('core_plugins',array('name'=>$item['onepluginname']),'title'):'公共分类';
			$item['issetonepluginname']=$pluginttitle?1:0;
			$item['grouplevel']=$shopset['level'];
			if($item['parentid']){
				$item['duanurl']='jy_weishop/goods.index//cid='.$item['id'];
				$item['url']=custom_url('jy_weishop/goods.index//cid='.$item['id'],true);
				$item['duanurl2']='jy_weishop/shop.groups//pid='.$item['parentid'].'&cid='.$item['id'];
			}else{
				$item['duanurl']='jy_weishop/goods.index//pid='.$item['id'];
				$item['url']=custom_url('jy_weishop/goods.index//pid='.$item['id'],true);
				$item['duanurl2']='jy_weishop/shop.groups//pid='.$item['id'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'一级分类'),
			array('op'=>'list2','title'=>$parenttitle2.'分类','query'=>'parentid='.$parentid2),
			array('op'=>'list3','title'=>$parenttitle.'分类'),
			array('op'=>'edit','title'=>'添加分类','query'=>'parentid='.$params['parentid']),
		),
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>true,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'id','title'=>'ID','type'=>'string'),
			array('name'=>'image','title'=>'分类图片','type'=>'image'),
			array('name'=>'title','title'=>'分类名称','type'=>'string'),
			array('name'=>'url','title'=>'分类链接','type'=>'copy'),
			array('name'=>'duanurl','title'=>'分类短链接','type'=>'copy'),
			array('name'=>'duanurl2','title'=>'分类短链接2','type'=>'copy'),
			array('name'=>'isshow','title'=>'是否显示','type'=>'switch','set'=>'set'),
			array('name'=>'isrecommand','title'=>'是否推荐(首页)','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'title','title'=>'空调分类名称','add'=>false),
	);
	$return['htmls']=$htmls;
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	exi($return);
}
//空调分类
if($_RQ['op']=='getgroups'){
	$return['list']=pdo_getall('jy_weishop_goods_group',array('parentid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	else{
		$item=array();
		if($_RQ['parentid']){
			$item['parentid']=intval($_RQ['parentid']);
			$item['parenttitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['parentid']),'title');
		}
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回一级分类列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string'),	
					array('name'=>'title','title'=>'分类名称','type'=>'string','must'=>true),
					array('name'=>'image','title'=>'LOGO','type'=>'image'),
					array('name'=>'showlevels','title'=>'会员等级浏览权限','type'=>'checkbox','options'=>'levels','help'=>'单空调优先：其次子分类优先'),
					array('name'=>'buylevels','title'=>'会员等级购买权限','type'=>'checkbox','options'=>'levels','help'=>'单空调优先：其次子分类优先'),
					array('name'=>'desc','title'=>'分类描述','type'=>'string'),
					array('name'=>'keywords','title'=>'分类关键词','type'=>'text'),
					array('name'=>'advimg','title'=>'分类广告','type'=>'image'),
					array('name'=>'advurl','title'=>'分类广告链接','type'=>'link','from'=>'action'),
					array('name'=>'isshow','title'=>'是否显示','type'=>'switch'),
					array('name'=>'isrecommand','title'=>'是否推荐','type'=>'switch','help'=>'推荐后才显示首页'),
				)),
		)
	);
	if($_RQ['parentid']){
		array_unshift($htmls['edits'][0]['list'],array('name'=>'parenttitle','title'=>'上级分类','type'=>'string','read'=>true));
	}
	$status=array('0'=>'显示','1'=>'隐藏');
	$return['status']=$status;
	$levels=pdo_getall('jy_weishop_member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']=array();
	$return['levels'][0]='默认会员';
	foreach($levels as $lv){
		$return['levels'][$lv['id']]=$lv['title'];
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('标题不能为空','error');
	}
	$data=array(
		'title'=>trim($postdata['title']),
		'desc'=>trim($postdata['desc']),
		'keywords'=>trim($postdata['keywords']),
		'image'=>trim($postdata['image']),
		'advimg'=>trim($postdata['advimg']),
		'advurl'=>trim($postdata['advurl']),
		'isshow'=>intval($postdata['isshow']),
		'paixu'=>intval($postdata['paixu']),
		'isrecommand'=>intval($postdata['isrecommand']),
		'showlevels'=>trim($postdata['showlevels']),
		'buylevels'=>trim($postdata['buylevels']),
	);
	$params['onepluginname']=$onepluginname;
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_goods_group',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['parentid']=intval($postdata['parentid']);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['onepluginname']=$onepluginname?:'';
		$data['pluginname']=$onepluginname?:'';
		pdo_insert('jy_weishop_goods_group',$data);
		$insertid=pdo_insertid();
	}
	exi('操作成功！','','goback');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['isshow'])){
		$update['isshow']=intval($post['isshow']);
	}
	if(isset($post['isrecommand'])){
		$update['isrecommand']=intval($post['isrecommand']);
	}
	if($update){
		pdo_update('jy_weishop_goods_group',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='uplegao'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择分类','error');
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_weishop_goods_group',array('onepluginname'=>$onepluginname),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('更新成功！');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods_group',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}